/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.system.infra.view;

import br.cesupa.fabsoft.nomedoprojeto.system.application.facades.ApplicationFacade;
import java.util.Enumeration;

/**
 * Handles localized message creation.
 *
 * @author Breno Leite
 */
public class MessageFactory {

    /**
     * Creates a {@link Message} object from a resource bundle. By default,
     * a message´s main text is localized using the sufix "_mainmessage".
     * If others keys with different sufix (like "_Details") are found, fields
     * are created in the message. The sufix (all text after "_") is translated
     * based on the word properties file.
     *
     * @param messageKey the key used to localize the message.
     * @param messageType the type of the message which will be created
     * @return a message
     */
    public Message createMessage(final String messageKey,
            final MessageType messageType) {
        Message message = new Message(translateApplicationMessage(messageKey),
                messageType);

        Enumeration<String> keySet = ApplicationFacade.getTranslationFacade().
                getApplicationStatusMessagesResourceBundle().getKeys();
        while (keySet.hasMoreElements()) {
            String key = keySet.nextElement();
            if (key.equals(messageKey + ".DETAILS")) {
                message.setDetails(translateApplicationMessage(key));
            }

            if (key.equals(messageKey + ".SOLUTION")) {
                message.setSolution(translateApplicationMessage(key));
            }
        }
        return message;
    }

    private String translateApplicationMessage(final String applicationMessageKey) {
        return ApplicationFacade.getTranslationFacade().
                translateApplicationStatusMessage(applicationMessageKey);
    }
}
